<template>
    <section>
        <!-- <el-form :inline="true" :model="formSearch" class="form_inline">
            <el-form-item label-width="80px" label="选择项目" size="small">
                <el-select v-model="formSearch.xqId" placeholder="请选择项目" @change="getlist(),pushChild()">
                    <el-option v-for="(item,index) in xqTree" :label="item.name" :value="item.id" :key="index">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label-width="80px" label="设备名称" >
                <el-input v-model="formSearch.deviceName" placeholder="设备名称"></el-input>
            </el-form-item>
            <el-form-item label-width="80px" label="区域" >
                <el-input v-model="formSearch.areaName" placeholder="区域"></el-input>
            </el-form-item>
            <el-form-item label-width="80px" label="状态" >
                <el-select v-model="formSearch.isOnline">
                    <el-option label="在线" :value="1"></el-option>
                    <el-option label="离线" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label-width="68px">
                <el-button  @click="getlist()">查 询</el-button>
            </el-form-item>
            <el-form-item label-width="68px">
                <el-button  v-if="formSearch.devType != 9&&formSearch.devType != 8&&$route.path!='/iot_device_point'"
                    @click.native="dialog=true,isAddorUpdate=1">添 加</el-button>
            </el-form-item>
            <el-form-item label-width="68px">
                <el-button  v-if="formSearch.devType == 9" @click.native="getSyncFushiDevice">同 步
                </el-button>
            </el-form-item>
        </el-form> -->
        <!-- ncu设备 -->
        <el-table :data="formData" style="width: 100%">
            <el-table-column prop="doorName" label="门禁名称"></el-table-column>
            <el-table-column prop="doorAddr" label="门禁地址"></el-table-column>
            <el-table-column prop="areaName" label="区域名"></el-table-column>
            <el-table-column prop="deviceName" label="设备名称"></el-table-column>
            <el-table-column label="是否在线">
                <template slot-scope="scope">
                    {{scope.row.isOnLine?'在线':'离线'}}
                </template>
            </el-table-column>
            <el-table-column label="是否打开">
                <template slot-scope="scope">
                    {{scope.row.isOpen?'打开':'关闭'}}
                </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width=250>
                <template slot-scope="scope">

                    <el-button type="success" size="small" @click="openDoor(scope.row)">开门</el-button>
                    <el-button type="info" size="small" @click="closeDoor(scope.row)">关门</el-button>
                </template>
            </el-table-column>
        </el-table>
        <paging @changePage='handleCurrentPage' :get-total='total'></paging>

        <!--  <addAndUpdate :father-infor='formUpdate' :father-is-addor-update='isAddorUpdate' :fatherFaceMachine="true"
            :father-active="formSearch.devType" :father-control-dialog.sync="dialog"
            @cancelControlDialog="cancelControlDialog" @refreshList="refreshList"></addAndUpdate> -->

    </section>
</template>

<script>
    import paging from '../../components/paging';
    import {
        fsRemoteOpenDoor,
        getAcDevlist,
        addAcDevlist,
        updateDoorDev,
        delAcDevlist,
        orgTree,
        xqSelectList,
        remoteOpenDoor,
        freshHwLevels,
        syncFushiDevice,
        getAkxDoors,
        handleAkxDoor,
    } from '../../url/api';

    export default {
        props: ['topActive'],
        data() {
            return {
                option1: [],
                xqTree: [],
                faceMachine: true,
                addDialog: false,
                isAddorUpdate: 1, //1添加、2修改
                formSearch: {
                    current: 1,
                    size: 10,
                    xqId: '',
                },
                formData: [], //数据
                formUpdate: {}, //修改表单
                total: 0, //数据总数
                pages: 0, //页面总数
                dialog: false,
                dataTree: [],
                defaultProps2: {
                    children: 'children',
                    label: 'name',
                    value: 'id',
                    expandTrigger: 'hover',
                    checkStrictly: true,
                },
                rules: {
                    name: [{ required: true, message: '该项不能为空' }],
                    xqId: [
                        {
                            required: true,
                            message: '该项不能为空',
                            trigger: 'change',
                        },
                    ],
                    orgId: [
                        {
                            required: true,
                            message: '该项不能为空',
                            trigger: 'blur',
                        },
                    ],
                },
                fushiData: [],
            };
        },
        created() {

            this.Bus.$on('sendTableData2', (data) => {
                console.log('~~~~', data);
                this.formSearch = data.selectdata,
                console.log("AAAAAAA",this.formSearch);
                this.handleCurrentPage(1);
                // this.getlist();
                    // this.total = data.total
            })
        },
        watch: {
            topActive(val) {
                //tabel切换
                console.log(val);
                this.formSearch.current = 1;
                if (this.formSearch.devType != val) {
                    this.formSearch.devType = val;

                    if (this.formSearch.devType == 8) {
                        freshHwLevels(this.formSearch.xqId).then((res) => {
                            //刷新霍尼门禁权限列表

                            console.log(res);

                            if (res.data.ok) {
                                this.formData = res.data.data;
                            }
                        });
                    } else {
                        this.getlist();
                    }
                }
            },
        },
        methods: {
            fushiOpenDoor(obj) {
                let params = {
                    devNo: obj.devNo,
                    doorNo: obj.doorNo,
                    xqId: obj.xqId,
                };
                this.$confirm('确认远程开门吗？')
                    .then((_) => {
                        fsRemoteOpenDoor(params).then((res) => {
                            console.log(res);
                            if (res.data.ok) {
                                this.$message({
                                    message: '远程开门成功',
                                    type: 'success',
                                });
                            } else {
                                this.$message(res.data.msg);
                            }
                        });
                        this.addDialog = false;
                    })
                    .catch((_) => { });
            },
            openDoor(obj) {

                let params = {
                    doorId: obj.doorId,
                    how: 'open',
                    xqId: this.formSearch.xqId
                };
                this.$confirm('确认开门吗？')
                    .then((_) => {
                        handleAkxDoor(params).then((res) => {
                            if (res.data.ok) {
                                this.$message({
                                    message: '远程开门成功',
                                    type: 'success',
                                });
                            } else {
                                this.$message(res.data.msg);
                            }
                        });
                    })
                    .catch((_) => { });
            },

            closeDoor(obj) {
                let params = {
                    doorId: obj.id,
                    how: 'close',
                };
                this.$confirm('确认关门吗？')
                    .then((_) => {
                        handleAkxDoor(params).then((res) => {
                            console.log(res);
                            if (res.data.ok) {
                                this.$message({
                                    message: '远程开门成功',
                                    type: 'success',
                                });
                            } else {
                                this.$message(res.data.msg);
                            }
                        });
                    })
                    .catch((_) => { });
            },
            cancelControlDialog(obj) {
                //关闭添加修改子组件
                this.dialog = false;
            },
            refreshList(obj) {
                //刷新列表
                console.log(obj);
                if (obj == true) {
                    this.getlist();
                }
            },
            pushChild() {
                this.$emit('changeXqid', this.formSearch.xqId);
            },
            // pushChild1(){
            //   this.$emit("changeXqid1",this.formSearch.xqId)
            // }豪方东园,
            //同步富士门禁设备
            getSyncFushiDevice() {
                syncFushiDevice(this.formSearch.xqId).then((res) => {
                    console.log(res);
                    if (res.data.ok) {
                        this.$message('富士门禁设备成功');
                        this.getlist();
                    } else {
                        this.$message('富士门禁设备失败');
                    }
                });
            },
            getlist() {
                getAkxDoors(this.formSearch).then((res) => {
                    console.log(res);
                    if (res.data.ok) {
                        this.formData = res.data.data.records.filter((item) => {
                            if (item.gmtCreate) {
                            }
                            return item;
                        });
                        this.total = res.data.data.total;
                    } else {
                        this.$message(res.data.msg);
                    }
                });
            },
            getInit() {
                //初始化列表

                xqSelectList({}).then((res) => {
                    //项目选择列表
                    console.log(res);
                    if (res.data.ok) {
                        this.xqTree = res.data.data;
                        this.formSearch.xqId = this.xqTree[0].id;
                        this.getlist();
                        this.pushChild();
                    }
                });

                xqSelectList({}).then((res) => { });
                let org_tree = {
                    name: '',
                    status: 1,
                };
                orgTree(org_tree).then((res) => {
                    //获取组织列表
                    console.log(res);
                    if (res.data.ok) {
                        this.dataTree = res.data.data;
                    }
                });
            },
            deleInfor(ids) {
                //删除
                let arrId = [];
                if (ids) {
                    arrId.push(ids);
                }
                this.$confirm('确认删除吗？')
                    .then((_) => {
                        delAcDevlist(arrId).then((res) => {
                            console.log(res);
                            if (res.data.ok) {
                                this.$message('删除成功');
                                this.getlist();
                            }
                        });
                    })
                    .catch((_) => { });
            },
            handleCurrentPage(val) {
                //页码改变

                this.formSearch.current = val;
                this.getlist();
            },
            updateShowBox(val, index) {
                //修改东西弹窗
                this.$nextTick(function () {
                    let value = JSON.parse(JSON.stringify(val));
                    let id = value.sectionId;
                    console.log(id);
                    this.formSearch.devType = index;
                    this.dialog = true;
                    this.isAddorUpdate = 2;
                    this.formUpdate = {
                        id: value.id,
                        name: value.name,
                        devSn: value.devSn,
                        devType: value.devType,
                        xqId: value.xqId,
                        doorkeySign: value.doorkeySign,
                        sectionId: id,
                        houseId: value.houseId,
                        embedType: value.embedType,
                        floor: value.floor,
                        ncuSn: value.ncuSn,
                        macValue: value.macValue,
                    };
                    console.log(this.formUpdate);
                });
            },
            handleChange(value) {
                if (value.length != 0) {
                    console.log(value);
                    this.formSearch.orgId =
                        this.$refs.cascader.getCheckedNodes()[0].data.id;
                } else {
                    this.formSearch.orgId = '';
                }
                getAcDevlist(this.formSearch).then((res) => {
                    console.log(res);
                    if (res.data.ok) {
                        this.formData = res.data.data.records.filter((item) => {
                            if (item.gmtCreate) {
                                // item.gmtCreate = this.$root.getDateArray(
                                //     item.gmtCreate
                                // )[9];
                            }
                            return item;
                        });
                        this.total = res.data.data.total;
                    } else {
                        this.$message(res.data.msg);
                    }
                });
            },
        },
        mounted() {
            this.getInit();
            console.log(this.$root.btnControl);
        },
        components: {
            // addAndUpdate,
            paging,
        },
    };
</script>
<style scoped>
    .cont_box {
        width: 100%;
        display: flex;
    }

    .cont_box_left,
    .cont_box_right {
        flex: 1;
        background: transparent;
        padding: 10px;
    }

    .cont_box_left {
        margin-right: 10px;
    }

    .margin10 {
        margin-bottom: 10px;
    }

    .marginRight {
        margin-bottom: 10px;
        border-bottom: 1px solid #eee;
    }

    .cont_box_right .el-form-item {
        margin-bottom: 20px;
    }
</style>
